package com.suray.wcs.service.thread;

import com.suray.basic.wcs.utils.WcsFuncUtil;
import com.suray.commin.core.utils.SpringContextHolder;
import com.suray.wcs.res.enums.RgvStatus;
import com.suray.wcs.service.enums.NotificationTypeEnum;
import com.suray.wcs.service.pojo.Rgv;
import com.suray.wcs.service.system.bo.NotificationBO;
import com.suray.wcs.service.system.service.impl.NotificationServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BatteryListenerThread extends Thread {

    private static BatteryListenerThread batteryListenerThread;

    private List<Rgv> rgvs;
    private Map<Rgv, Boolean> rgvNotifiLock;

    private BatteryListenerThread() {
        this.rgvs = new ArrayList<>();
        rgvNotifiLock = new HashMap<>();
    }

    public static synchronized BatteryListenerThread getBatteryListenerThread() {
        if (batteryListenerThread == null) {
            batteryListenerThread = new BatteryListenerThread();
        }

        return batteryListenerThread;
    }

    public void addRgv(Rgv rgv) {
        rgvs.add(rgv);
    }

    private void lockRgvNotifi(Rgv rgv) {
        rgvNotifiLock.put(rgv, true);
    }

    private void unlockRgvNotifi(Rgv rgv) {
        rgvNotifiLock.put(rgv, false);
    }

    private boolean isLockRgvNotifi(Rgv rgv) {
        Boolean isLock = rgvNotifiLock.get(rgv);
        return isLock == null ? false : isLock;
    }

    @Override
    public void run() {
        while (true) {
            try {
                batteryListener();
            } catch (Exception e) {
                WcsFuncUtil.exceptionLog(e, this.getClass());
            }

            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void batteryListener() {
        rgvs.forEach((rgv) -> {
            if (rgv.getBattery() != null) {
                if ((rgv.getRgvStatus() != RgvStatus.OFF_LINE && rgv.getRgvStatus() != RgvStatus.CHARGING)
                        && rgv.getBattery() < rgv.getRgvDB().getAlarmBatterySize()) {
                    if (!isLockRgvNotifi(rgv)) {
                        NotificationBO notificationBO = new NotificationBO();
                        notificationBO.setContent(rgv.getRgvName() + "，当前电量"+rgv.getBattery()+"%低于"+rgv.getRgvDB().getAlarmBatterySize()+"%，请及时充电");
                        notificationBO.setNotificationType(NotificationTypeEnum.WARN.getType());
                        notificationBO.setIsRead(false);
                        notificationBO.setSeverityLevel(1);
                        NotificationServiceImpl notificationService = SpringContextHolder.getBean(NotificationServiceImpl.class);
                        notificationService.insertNotification(notificationBO);
                        lockRgvNotifi(rgv);
                    }
                } else {
                    if (rgv.getBattery() > rgv.getRgvDB().getAlarmBatterySize() && isLockRgvNotifi(rgv)) {
                        unlockRgvNotifi(rgv);
                    }
                }
            }
        });
    }
}
